<?php

// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------


namespace app\index\controller;

use app\index\BaseController;
use Exception;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;
use think\facade\Request;


/**
 * Class Auth
 * @package app\controller\api
 * @author xaboy
 * @day 2020-05-06
 */
class Common extends BaseController
{
    //给单独页面调用获取数据
    function getStationData(){
        $data =  Request::post();
        if(!isset($data['station_id']) || !$data['station_id']) $data['station_id'] = 1;
        $commonWhere = ['station_id'=>$data['station_id']];
        //设置站点信息
        $getStationInfo = Db::table('jy_station_data')->where($commonWhere)->withoutField('create_time,update_time')->find();
        $gunNumber = $getStationInfo['gun_number'] ? $getStationInfo['gun_number'] : 20;
        $userRatio = round(($getStationInfo['gun_use'] / $gunNumber) * 100);
        $getStationInfo['use_ratio'] = $userRatio."%";
        $getStationInfo['free_ratio'] = (100 - $userRatio)."%";
        $getStationInfo['error_ratio'] = "0%";
        $getStationInfo['station_number'] = 1;
        $getStationInfo['gun_number'] = $gunNumber;
        $stationInfo =  Db::table('jy_charging_station')->where('id',$data['station_id'])->find();
        $getStationInfo['longitude'] = 113.356;
        $getStationInfo['latitude'] = 22.4866;
        if($stationInfo){
            $getStationInfo['longitude'] = $stationInfo['longitude'];
            $getStationInfo['latitude'] = $stationInfo['latitude'];
        }
        $getStationInfo['all_charge_electricity'] =  bcmul($getStationInfo['real_charge_electricity'] , 1.1 ,2);
        $allMoney = bcmul($getStationInfo['real_service_cost'] , (1.1 * 1.1),2 )+ bcmul($getStationInfo['real_charge_cost'],1.1,2);
        $getStationInfo['all_charge_money'] =  bcmul($allMoney,1,2);
        $getStationInfo['all_charge_time'] =  $getStationInfo['real_charge_time'];
        $withOutField = 'create_time,update_time,carbon_dioxide_number,tree_planting_number';
        //获取今天的信息
        $dayInfo =  Db::table('jy_station_day_data')->where($commonWhere)->where('day_data',date("Y-m-d"))->withoutField($withOutField)->find();
        if($dayInfo){
            $getStationInfo['today_charge_electricity'] = bcmul($dayInfo['real_charge_electricity'] , 1.1 ,2);
            $todayMoney = bcmul($dayInfo['real_service_cost'] , (1.1 * 1.1),2 )+ bcmul($dayInfo['real_charge_cost'],1.1,2);
            $getStationInfo['today_charge_money'] = bcmul($todayMoney,1,2);
        }else{
            $getStationInfo['today_charge_electricity'] = 0;
            $getStationInfo['today_charge_money'] = 0;
        }
        //获取昨天前天信息
        $lastDay = date("Y-m-d",strtotime("-1 day"));
        $lastWhere = ['day_data'=>$lastDay];
        /*
        $getLastData = Db::table('jy_station_day_data')->where($commonWhere)->where($lastWhere)->find();
        $secondDay = date("Y-m-d",strtotime("-2 day"));;
        $secondWhere = ['day_data'=>$secondDay];
        $getSecondData = Db::table('jy_station_day_data')->where($commonWhere)->where($secondWhere)->find();
        */

        $getList = Db::table('jy_station_day_data')->where($commonWhere)->order('id','desc')->withoutField($withOutField)->page(1,3)->select()->toArray();
        $getLastData = isset($getList[1]) ? $getList[1] : '';
        $getSecondData = isset($getList[2]) ? $getList[2] : '';

        //昨天不存在数据
        if(!$getLastData){
            $getLastData = [
                'charge_electricity'=>0,
                'charge_money'=>0,
                'charge_number'=>0,
                'charge_time'=>0,
                'charge_electricity_ratio'=>0,
                'charge_money_ratio'=>0,
                'charge_number_ratio'=>0,
                'charge_time_ratio'=>0,
                ];
        }else{
            $getLastData['charge_electricity'] = bcmul($getLastData['real_charge_electricity'] , 1.1 ,2);
            $getLastData['charge_money'] = bcmul($getLastData['real_service_cost'] , (1.1 * 1.1),2 )+ bcmul($getLastData['real_charge_cost'],1.1,2);
            $getLastData['charge_time'] = $getLastData['real_charge_time'];
            if($getSecondData){
                $getSecondData['charge_electricity'] = bcmul($getSecondData['real_charge_electricity'] , 1.1 ,2);
                $getSecondData['charge_money'] = bcmul($getSecondData['real_service_cost'] , (1.1 * 1.1),2 )+ bcmul($getSecondData['real_charge_cost'],1.1,2);
                $getSecondData['charge_time'] = $getSecondData['real_charge_time'];
                if($getSecondData['charge_electricity'] > 0){
                    $getLastData['charge_electricity_ratio'] = round((($getLastData['charge_electricity'] - $getSecondData['charge_electricity'])/$getSecondData['charge_electricity']) * 100)."%";
                }else{
                    $getLastData['charge_electricity_ratio'] = '100%';
                }
                if($getSecondData['charge_money'] > 0){
                    $getLastData['charge_money_ratio'] = round((($getLastData['charge_money'] - $getSecondData['charge_money'])/$getSecondData['charge_money']) * 100)."%";
                }else{
                    $getLastData['charge_money_ratio'] = '100%';
                }
                if($getSecondData['charge_number'] > 0){
                    $getLastData['charge_number_ratio'] = round((($getLastData['charge_number'] - $getSecondData['charge_number'])/$getSecondData['charge_number']) * 100)."%";
                }else{
                    $getLastData['charge_number_ratio'] = '100%';
                }
                if($getSecondData['charge_time'] > 0){
                    $getLastData['charge_time_ratio'] = round((($getLastData['charge_time'] - $getSecondData['charge_time'])/$getSecondData['charge_time']) * 100)."%";
                }else{
                    $getLastData['charge_time_ratio'] = '100%';
                }
            }else{
                $getLastData['charge_electricity_ratio'] = '100%';
                $getLastData['charge_money_ratio'] = '100%';
                $getLastData['charge_number_ratio'] = '100%';
                $getLastData['charge_time_ratio'] = '100%';
            }
        }

        //排行榜
        $orderList = Db::table('jy_station_day_data')->where($lastWhere)->order('charge_electricity','desc')
            ->page(1,10)->select()->each(function ($item){
                $item['station_name'] = Db::table('jy_charging_station')->where('id',$item['station_id'])->value('title');
                $item['charge_electricity'] = bcmul($item['real_charge_electricity'] , 1.1 ,2);
                $item['charge_money'] = bcmul($item['real_service_cost'] , (1.1 * 1.1),2 )+ bcmul($item['real_charge_cost'],1.1,2);
                $item['charge_time'] = $item['real_charge_time'];
                return $item;
            })->toArray();
        $backData = [
            'station_info'=>$getStationInfo,
            'last_station_info'=>$getLastData,
            'order_list'=>$orderList,
        ];
        return $this->success('获取成功',$backData);
    }

    /**
     * 上传图片
     * @return $this
     */
    function uploadImg(){
        $getTypeName = $getData = Request::get('imgType') ? Request::get('imgType') : 'img';
        $file = $this->request->file("file");
        $dirDay = date("Y")."/".date("m");
        $fieldName = \think\facade\Filesystem::disk('index')->putFile("/webImg/".$getTypeName."/".$dirDay,$file);
        $backImg = [
            'showImg'=>config('add.web_url')."/upload/".$fieldName,
            'uploadImg'=>"/upload/".$fieldName,
        ];
        if($fieldName){
            return $this->success('上传成功',$backImg);
        }else{
            return $this->fail('上传失败');
        }
    }




}
